//
//  CYAddCarTableVC.m
//  CreditAddressBook
//
//  Created by LE on 15/12/17.
//  Copyright © 2015年 Lee. All rights reserved.
//

#import "CYAddCarTableVC.h"
#import "LEProgressButton.h"
#import "LEBarButtonItem.h"
#import "CYDiscoveryHttpTool.h"
#import "MBProgressHUD+MJ.h"
#import "LEImagePicker.h"
#import "CYCarInfo.h"
#import "CYLoginHttpTool.h"
#import "CYCompanyUserTool.h"
#import "LEImagePickerTool.h"

@interface CYAddCarTableVC ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *carLicenseTF;
@property (weak, nonatomic) IBOutlet UITextField *carModelTF;
@property (weak, nonatomic) IBOutlet UITextField *engineNumTF;
@property (weak, nonatomic) IBOutlet UITextField *vehicleLicenceNumTF;

@property(nonatomic, strong) LEImagePicker *imagePicker;
@property(nonatomic, strong) CYCarInfo *carInfo;

@property(nonatomic, copy) CYCompletionBlock completionBlock;
@end

@implementation CYAddCarTableVC

- (CYCarInfo *)carInfo {
    if (!_carInfo) {
        _carInfo = [CYCarInfo new];
    }
    return _carInfo;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self submitBtn];
    self.imagePicker = [[LEImagePicker alloc] init];
}

- (void)submitBtn {
    LEBarButtonItem *item = [[LEBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Checked Checkbox 2-50"] andSelImage:[UIImage imageNamed:@"Checked Checkbox 2-50_h"] target:self action:@selector(submit)];
    self.navigationItem.rightBarButtonItem = item;
}

- (void)submit {
    if ([self setupCarInfo]) {
        CYWeakObject(weakself, self);
        [CYDiscoveryHttpTool addCarInfoWithParam:self.carInfo success:^(NSString *message) {
            [MBProgressHUD showSuccess:message];
            //刷新上一个界面
            if (weakself.completionBlock) {
                weakself.completionBlock();
            }
            weakself.completionBlock = nil;
            [weakself.navigationController popViewControllerAnimated:YES];
        } failure:^(NSString *message, NSError *error) {
            if (message) {
                [MBProgressHUD showError:message];
            }
        }];
    }
}

- (BOOL)setupCarInfo {
    if (self.carLicenseTF.text && !self.carLicenseTF.text.isEmpty) {
        self.carInfo.plateNumber = self.carLicenseTF.text;
    } else {
        [MBProgressHUD showError:@"请输入车牌号"];
        return NO;
    }
    if (self.carModelTF.text && !self.carModelTF.text.isEmpty) {
        self.carInfo.vehicleType = self.carModelTF.text;
    } else {
        [MBProgressHUD showError:@"请输入车型号"];
        return NO;
    }
    if (self.engineNumTF.text && !self.engineNumTF.text.isEmpty) {
        self.carInfo.carEngineNumber = self.engineNumTF.text;
    } else {
        [MBProgressHUD showError:@"请输入发动机型号"];
        return NO;
    }
    if (self.vehicleLicenceNumTF.text && !self.vehicleLicenceNumTF.text.isEmpty) {
        self.carInfo.carFrameNumber = self.vehicleLicenceNumTF.text;
    } else {
        [MBProgressHUD showError:@"请输入车架号"];
        return NO;
    }
    if (!self.carInfo.photo || self.carInfo.photo.isEmpty) {
        [MBProgressHUD showError:@"请上传车辆照片"];
        return NO;
    }
    if (!self.carInfo.vehicleLicence || self.carInfo.vehicleLicence.isEmpty) {
        [MBProgressHUD showError:@"请上传行车证照片"];
        return NO;
    }
    self.carInfo.type = @(self.userType);
    if (self.userType == CYUserTypePerson) {
        CYUser *user = [CYLoginHttpTool userData].user;
        if (user) {
            self.carInfo.userId = user.userId;
        } else {
            [MBProgressHUD showError:@"请登录后再进行操作"];
            return NO;
        }
    } else if (self.userType == CYUserTypeCompany) {
        CYCompanyUserModel *user = [CYCompanyUserTool companyUser];
        if (user) {
            self.carInfo.userId = user.carCompanyUserId;
        } else {
            [MBProgressHUD showError:@"请登录后再进行操作"];
            return NO;
        }
    }
    return YES;
}

- (IBAction)uploadCarPhoto:(LEProgressButton *)sender {
    [self.view.window endEditing:YES];
    CYWeakObject(weakself, self);
    [LEImagePickerTool selectImageWithController:self edit:YES completion:^(UIImage *image) {
        [CYDiscoveryHttpTool uploadUserImage:image button:sender success:^(NSString *imagePath) {
            weakself.carInfo.photo = imagePath;
        } failure:^(NSString *message, NSError *error) {
            if (message) {
                [MBProgressHUD showError:message];
            }
        }];
    }];
//    [self.imagePicker showInController:self handler:^(UIImage *image) {
//        [CYDiscoveryHttpTool uploadUserImage:image button:sender success:^(NSString *imagePath) {
//            weakself.carInfo.photo = imagePath;
//        } failure:^(NSString *message, NSError *error) {
//            if (message) {
//                [MBProgressHUD showError:message];
//            }
//        }];
//    }];
}
- (IBAction)uploadVehicleLicencePhoto:(LEProgressButton *)sender {
    [self.view.window endEditing:YES];
    CYWeakObject(weakself, self);
    [LEImagePickerTool selectImageWithController:self edit:YES completion:^(UIImage *image) {
        [CYDiscoveryHttpTool uploadUserImage:image button:sender success:^(NSString *imagePath) {
            weakself.carInfo.vehicleLicence = imagePath;
        } failure:^(NSString *message, NSError *error) {
            [MBProgressHUD showError:message];
        }];
    }];
//    [self.imagePicker showInController:self handler:^(UIImage *image) {
//        [CYDiscoveryHttpTool uploadUserImage:image button:sender success:^(NSString *imagePath) {
//            weakself.carInfo.vehicleLicence = imagePath;
//        } failure:^(NSString *message, NSError *error) {
//            [MBProgressHUD showError:message];
//        }];
//    }];
}

#pragma mark - Table view data source

#pragma mark - UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField.text && !textField.text.isEmpty) {
        [CYDiscoveryHttpTool checkPlateNumberWithPlateNumber:textField.text success:^(NSString *message) {
            
        } failure:^(NSString *message, NSError *error) {
            if (message) {
                [MBProgressHUD showError:message];
                textField.text = nil;
            }
        }];
    }
}

- (void)completion:(CYCompletionBlock)completion {
    self.completionBlock = completion;
}
@end
